<template>
  <div class="base-form" style="width: 800px;">
    <!--S表单-->
    <el-form
      ref="settingForm"
      class="setting-form"
      :rules="formDataRules"
      :model="formData"
      label-position="left"
      label-width="100px"
    >
      <el-form-item label="公众号名称" prop="wechat_name">
        <el-input v-model="formData.wechat_name" placeholder="平台名称" />
      </el-form-item>
      <el-form-item label="Appid" prop="wechat_appid">
        <el-input v-model="formData.wechat_appid" placeholder="公众号Appid" />
      </el-form-item>
      <el-form-item label="AppSecret" prop="wechat_secret">
        <el-input v-model="formData.wechat_secret" placeholder="公众号AppSecret" />
      </el-form-item>
      <el-form-item label="公众号Logo" prop="wechat_logo">
        <select-attachment v-model="formData.wechat_logo" />
      </el-form-item>
      <el-form-item label="公众号二维码" prop="wechat_qrcode">
        <select-attachment v-model="formData.wechat_qrcode" />
      </el-form-item>
      <el-button type="primary" size="small" @click="submit">确认配置</el-button>
    </el-form>
    <!--E表单-->
  </div>
</template>

<script>
import SelectAttachment from '@/components/SelectAttachment/index'
export default {
  name: 'BaseForm',
  components: {
    SelectAttachment
  },
  props: {
    setting: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        wechat_name: '',
        wechat_appid: '',
        wechat_secret: '',
        wechat_logo: [],
        wechat_qrcode: []
      },
      formDataRules: {
        wechat_name: [
          { required: true, message: '请输入平台名称', trigger: 'blur' },
          { max: 30, message: '长度不能超30个字', trigger: 'blur' }
        ],
        wechat_appid: [
          { required: true, message: '请输入appid', trigger: 'blur' }
        ],
        wechat_secret: [
          { required: true, message: '请输入secret', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    setting() {
      const { wechat_name, wechat_appid, wechat_secret, wechat_logo, wechat_qrcode } = this.setting
      this.formData = {
        wechat_name,
        wechat_appid,
        wechat_secret,
        wechat_logo: wechat_logo ? [wechat_logo] : [],
        wechat_qrcode: wechat_qrcode ? [wechat_qrcode] : []
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.settingForm.validate((valid) => {
        if (valid) {
          const { wechat_name, wechat_appid, wechat_secret, wechat_logo, wechat_qrcode } = this.formData
          this.$put('setting/wechat', {
            wechat_name,
            wechat_appid,
            wechat_secret,
            wechat_logo: wechat_logo.length > 0 ? wechat_logo[0] : '',
            wechat_qrcode: wechat_qrcode.length > 0 ? wechat_qrcode[0] : ''
          })
            .then(() => {
              this.$message.success({
                message: '修改成功',
                type: 'success'
              })
            })
            .catch((error) => {
              this.$handleError(error)
            })
        }
      })
    }
  }
}
</script>
